package com.example.controller;

import com.example.service.PoetryService;
import jakarta.annotation.Resource;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


@RestController
@CrossOrigin
public class PoetryDetailController {
    @Resource
    private PoetryService poetryService;

    @GetMapping("/poetry/{id}")//诗人详情
    public HashMap<String,Object> poetry_detail(@PathVariable int id) {//url中接收诗人Id
        HashMap<String,Object> hashmap=new HashMap<>();
        String url=poetryService.detail_url(id);
        //poetryService.create_index();//为id添加索引
        try {
            // 使用Jsoup连接到指定的网址，并获取HTML内容
            Document document = Jsoup.connect(url).get();
            JXDocument jxDocument = JXDocument.create(document);

            hashmap.put("name",document.getElementsByTag("h2").get(0).text());
            hashmap.put("img",document.getElementsByClass("touxiang").select("img[src]").attr("src"));

            Element firstP = document.select("h2").first();
            hashmap.put("time",firstP.nextSibling().toString().trim());  //<h2>标签外面的文字

            String summary="";
            List<JXNode> jxNodes = jxDocument.selN("//*[@id=\"leftdg\"]/div[1]/div[1]/div/a"); //寻找简介内的a元素（链接）
            if(!jxNodes.isEmpty()){
                if (jxNodes.get(0).asElement().previousSibling()!=null){//若a非首元素 字符串连接该元素之前的文字
                    summary+=jxNodes.get(0).asElement().previousSibling().toString().trim();
                }
                for(JXNode jxNode : jxNodes){
                    Element element = jxNode.asElement();
                    summary+=element.text()+element.nextSibling().toString().trim();//连接之后的文字 用以去除三角形
                }
            }
            hashmap.put("summary",summary);

            int i;
            List<List> poems=new ArrayList<>();
            for(i=0;i<document.getElementsByTag("h2").size();i++){
                if (document.getElementsByTag("h2").get(i).text().contains("的诗文")){
                    //System.out.println(document.getElementsByTag("h2").get(i).text());
                    break;
                }
            }
            Element ele=document.getElementsByTag("h2").get(i).parent();//定位其父元素
            while(ele.nextElementSibling()!=null){
                List<List> poem=new ArrayList<>();
                if(ele.nextElementSibling().select("h2").text().contains("查看更多")){
                    break;
                }
                if(ele.nextElementSibling().select("h2").text()!=""){
                    List<String> poem_name=new ArrayList<>();
                    poem_name.add(ele.nextElementSibling().select("h2").text());//诗文名称
                    poem.add(poem_name);
                    String text=ele.nextElementSibling().getElementsByTag("div").get(3).text();
                    List<String> sentence= Arrays.asList(text.split("(?<=[。？！])"));
                    poem.add(sentence);
                    poems.add(poem);
                }
                ele=ele.nextElementSibling();
            }
            hashmap.put("poems",poems);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return hashmap;
    }
}
